<template>
  <div>
    <div>
      <FormPanel ref="formPanel" :list="formList" :cols="2" :labelWidth="110" :isSubmitBtn="false" @formChange="formChangeHandle" />
    </div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <AjaxButton type="primary" :click="saveDialog">确 定</AjaxButton>
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import { addLibraries } from '@/api/part/Warehouse';
import { getBrands } from '@/api/part/Warehouse';
// import { getStroageList } from '@/api/part/partOutbound/partBreak';
import { getStorageList } from '@/api/part/common';
import { notifyAction } from '@/utils';

export default {
  name: 'addMoreModel',
  mixins: [dictionary],
  props: {
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    this.conf = { add: '保 存', edit: '修 改' };
    this.saveData = null;
    this.flag = false;
    return {
      formList: this.createFormList(),
      tempBeforeString: 0
    };
  },
  computed: {},
  mounted() {
    this.init();
    this.getBrands();
  },
  methods: {
    init() {},
    createFormList() {
      return [
        {
          type: 'SELECT',
          label: '仓库名称',
          fieldName: 'storageCode',
          placeholder: '请选择',
          itemList: [],
          rules: { required: true, message: '请选择仓库', trigger: 'change' }
        },
        {
          type: 'INPUT',
          label: '前导字符',
          fieldName: 'beforeString',
          placeholder: '请输入...',
          change: val => {
            console.log('前导字符', val);
            this.tempBeforeString = val.length;
            console.log('前导字符', this.tempBeforeString);
          }
          // rules: { required: true, message: '请输入前导字符', trigger: 'change' }
        },
        {
          type: 'INPUT_NUMBER',
          label: '起始编号',
          fieldName: 'beginNo',
          placeholder: '请输入...',
          rules: { required: true, message: '请输入起始编号', trigger: 'change' }
        },
        {
          type: 'INPUT_NUMBER',
          label: '编号位数',
          fieldName: 'countNo',
          min: 3,
          placeholder: '请输入...',
          rules: [
            { required: true, message: '请输入编号位数', trigger: 'change' },
            {
              validator: (rule, value, callback) => {
                console.log(Number(value) + this.tempBeforeString, 12);

                if (Number(value) + this.tempBeforeString > 30) {
                  // console.log(value);
                  return callback(new Error('请确认前导字符个数与编号位数相加不能超过30位'));
                } else {
                  callback();
                }
              },
              trigger: 'change'
            }
          ]
        },
        {
          type: 'INPUT_NUMBER',
          label: '形成数量',
          fieldName: 'count',
          placeholder: '请输入...',
          min: 1,
          rules: { required: true, message: '请输入形成数量', trigger: 'change' }
        }
      ];
    },
    formChangeHandle(val) {
      this.saveData = val;
    },
    // 提交
    async saveDialog() {
      this.$refs.formPanel.SUBMIT_FORM();
      if (!this.saveData) return;
      let res = {};
      res = await addLibraries(this.saveData);
      if (res.resultCode == 200) {
        notifyAction('添加成功', 'success');
        this.researchMainTable();
        this.closeDialog();
      }
    },
    async getBrands() {
      const res = await getStorageList({});
      // 增加非空判断
      if (res.data.length) {
        this.formList.find(item => item.fieldName === 'storageCode').itemList = res.data.map(x => ({ text: x.STORAGE_NAME, value: x.STORAGE_CODE }));
      }
    },
    closeDialog(param) {
      this.$emit('close', false, param);
    },
    researchMainTable() {
      this.$emit('researchTable');
    }
  }
};
</script>

<style lang="less" scoped></style>
